<template>
  <div class="attrs-info">
    <!-- title弹窗标题 -->
    <!-- visible是否显示 -->
    <el-dialog :title="info.title" :visible.sync="info.isShow" @close="close">
      <!-- model:表单的数据 -->
      <!-- rules：检验的规则 -->
      <el-form :model="formData" label-width="100px" ref="formData">
        <el-form-item label="属性名" prop="specsname">
          <el-input v-model="formData.specsname"></el-input>
        </el-form-item>
        <el-form-item label="属性值" prop="attrs">
          <div v-for="(item, index) in attrsArr" :key="index">
            <el-input class="attrs" v-model="attrsArr[index]"></el-input>
            <el-button v-if="index === 0" type="primary" @click="addAttrs"
              >添加属性</el-button
            >
            <el-button v-else type="danger" @click="delAttrs(index)"
              >删除属性</el-button
            >
          </div>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { specsAdd, specsEdit } from "../../api";
export default {
  props: ["info"],
  data() {
    return {
      // 表单的数据
      formData: {},
      // 属性值对应的数组
      attrsArr: [""],
    };
  },
  created() {},
  methods: {
    setValue(data) {
      // 将data中的数据放入attrsArr中，使得属性值的按钮和输入框开始循环
      this.attrsArr = data.attrs
      // 设置弹窗中的数据
      this.formData = { ...data };
    },
    // 增加表单项
    addAttrs() {
      if (this.attrsArr.length == 5) {
        this.$message({
          message: "属性值最多5个",
          type: "warning",
        });
      } else {
        this.attrsArr.push("");
      }
    },
    // 删除表单项
    delAttrs(index) {
      this.attrsArr.splice(index, 1);
    },
    // 点击确认按钮，提交数据，关闭弹窗
    submitForm() {
      // this.$refs选择节点
      // validate() 是element ui的自带的检验方法
      this.$refs.formData.validate(async (valid) => {
        let res;
        this.formData.attrs = this.attrsArr.join();
        if (valid) {
          // 判断新增还是修改
          if (this.formData.id) {
            // 有id说明是修改
            res = await specsEdit(this.formData);
          } else {
            // 新增
            res = await specsAdd(this.formData);
          }
          if (res.code === 200) {
            // 操作成功
            this.$message({
              message: res.msg,
              type: "success",
            });
            // 关闭弹窗
            this.$emit("close");
            this.$emit("update");
          }
        } else {
          console.log("err");
          return false;
        }
      });
    },
    // 关闭弹窗
    cancel() {
      // 子传父，发射自定义事件，通知父组件关闭弹窗
      this.$emit("close");
    },
    // ui框架关闭弹窗
    close() {
      // 关闭弹窗时清空数组，而且清空勾选的权限。避免数据影响
      this.formData.menus = [];
    },
  },
};
</script>

<style>
.attrs-info .attrs {
  width: 270px;
  margin-right: 20px;
  margin-bottom: 10px;
}
</style>